package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;



    //根据用户名查询数据库获取用户信息和角色信息还有权限
    @Override
    public User findByUserName(String userName) {
        User user = userDao.findByUserName(userName);
        if(user == null){
            return null;
        }
        //根据用户id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(user.getId());

        for (Role role : roles) {
            //根据角色查询权限
            Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissions);//让角色跟权限关联

        }

        user.setRoles(roles);
        return user;
    }

}
